package com.jyw.system.utils;

import cn.hutool.core.util.StrUtil;
import com.jyw.common.constant.ConfigKeyConstant;
import com.jyw.common.core.domain.model.CacheVersion;
import com.jyw.common.utils.spring.SpringUtils;
import com.jyw.system.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.stereotype.Component;
import org.springframework.util.StringValueResolver;

/**
 * @author 沈峻
 * @ClassName CacheVersionUtils
 * @Description 缓存版本工具类
 * @Date 2023/8/25 14:34
 **/
@Component
@Slf4j
public class CacheVersionUtils implements EmbeddedValueResolverAware {

    private static StringValueResolver stringValueResolver;

    /**
     * 获取程序版本
     * @return
     */
    private static String getProjectVersion(){
        //获取配置上下文
        String property = getPropertiesValue("jyw.version");
        return property;
    }

    /**
     * 获取字典版本号
     * @return
     */
    private static String getDictVersion(){
        ISysConfigService configService = getConfigService();
        return configService.selectConfigByKey(ConfigKeyConstant.DICT_CACHE_VERSION);
    }

    /**
     * 升级字典版本号
     * @return
     */
    public static void dictVersionUp(){
        ISysConfigService configService = getConfigService();
        configService.versionUp(ConfigKeyConstant.DICT_CACHE_VERSION);
    }

    /**
     * 获取用户版本号
     * @return
     */
    private static String getUserVersion(){
        ISysConfigService configService = getConfigService();
        return configService.selectConfigByKey(ConfigKeyConstant.USER_CACHE_VERSION);
    }

    /**
     * 升级用户版本号
     * @return
     */
    public static void userVersionUp(){
        ISysConfigService configService = getConfigService();
        //升级版本号
        configService.versionUp(ConfigKeyConstant.USER_CACHE_VERSION);
        //清理用户缓存
        UserUtils.cleanCache();
    }

    /**
     * 获取科室版本号
     * @return
     */
    private static String getDeptVersion(){
        ISysConfigService configService = getConfigService();
        return configService.selectConfigByKey(ConfigKeyConstant.DEPT_CACHE_VERSION);
    }

    /**
     * 升级科室版本号
     * @return
     */
    public static void deptVersionUp(){
        ISysConfigService configService = getConfigService();
        //升级科室版本号
        configService.versionUp(ConfigKeyConstant.DEPT_CACHE_VERSION);
        //清理科室缓存
        DeptUtils.cleanCache();
    }

    /**
     * 获取小组版本号
     * @return
     */
    private static String getTeamVersion(){
        ISysConfigService configService = getConfigService();
        return configService.selectConfigByKey(ConfigKeyConstant.DICT_CACHE_VERSION);
    }

    /**
     * 升级小组版本号
     * @return
     */
    public static void teamVersionUp(){
        ISysConfigService configService = getConfigService();
        configService.versionUp(ConfigKeyConstant.TEAM_CACHE_VERSION);
        //清理小组缓存
        TeamUtils.cleanCache();
    }

    /**
     * 获取缓存管理版本号
     * @return
     */
    public static CacheVersion getVersion(){
        CacheVersion ver = new CacheVersion();
        String projectVersion = getProjectVersion();
        ver.setUser(StrUtil.format("{}{}{}",projectVersion,".",getUserVersion()));
        ver.setDept(StrUtil.format("{}{}{}",projectVersion,".",getDeptVersion()));
        ver.setTeam(StrUtil.format("{}{}{}",projectVersion,".",getTeamVersion()));
        ver.setDict(StrUtil.format("{}{}{}",projectVersion,".",getDictVersion()));
        return ver;
    }



    /**
     * 获取系统参数服务类
     * @return
     */
    private static ISysConfigService getConfigService(){
        return SpringUtils.getBean(ISysConfigService.class);
    }

    /**
     * 动态获取配置文件中的值
     * @param name
     * @return
     */
    private static String getPropertiesValue(String name) {
        try {
            name = "${" + name + "}";
            return stringValueResolver.resolveStringValue(name);
        } catch (Exception e) {
            log.error(String.format("当前环境变量中没有{%s}的配置", name));
            // 获取失败则返回null
            return null;
        }
    }

    @Override
    public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {
        this.stringValueResolver = stringValueResolver;
    }
}
